<?php
// Определяем корневой путь
if(isset($_SERVER['HTTP_HOST'])){
  if ($_SERVER['HTTP_HOST'] == 'localhost')
    $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];//Этот путь будет использоваться при работе с localhost
  elseif ( ($_SERVER['HTTP_HOST'] == '91.189.81.78') || ($_SERVER['HTTP_HOST'] == 'bitbybit.ru') || ($_SERVER['HTTP_HOST'] == 'www.bitbybit.ru'))//Этот путь будет использоваться в сети
    $DOCUMENT_ROOT = '/home/bitbybit/domains/bitbybit.ru/public_html/';
}
else
  $DOCUMENT_ROOT = '';//Этот путь будет использоваться при работе отладчика Зенда (для корректной работы нужно внести правки в конфигурационный файл для ABSPATH)
require_once($DOCUMENT_ROOT . '../bitbybit_start.php');//Подключаем файл инициализации
$columnRight = false;
$headTitle = 'Статистика';
$arrStatistics = Statistics::getStatistics();
$arrStatisticsSort[0] = Statistics::getNumDayStatistics(0, 24);
$arrStatisticsSort[0]['type'] = 0;
$arrStatisticsSort[1] = Statistics::getNumDayStatistics(1, 7);
$arrStatisticsSort[1]['type'] = 1;
$arrStatisticsSort[2] = Statistics::getNumDayStatistics(2, 12);
$arrStatisticsSort[2]['type'] = 2;
//$arrStatisticsSort[3] = Statistics::getNumDayStatistics(3, 12);
//$arrStatisticsSort[3]['type'] = 3;
$headCss = '<link rel="stylesheet" href="' . W_CSS . 'glavnaia.css" type="text/css" media="screen" />';
$headJS = '<script type="text/javascript" src="' . W_JS . 'fusionCharts.js"></script>';
include(TEMPLATE . 'mainHead.php');//подключаем основные части стриницы
echo '<h1>Статистика</h1>';
echo '<br />';
View_Statistics::displayStatistics($arrStatistics);
foreach ($arrStatisticsSort as $arrStatistics){
   # Create Column 3D + Line Dual Y-Axis Combination Chart
   $FC = new FusionCharts("MSColumn2D"/*MSLine*/,"850","400");

   # Set the relative path of the swf file
   $FC->setSWFPath(W_ABSPATH . 'content/swf/');
	 
   switch ($arrStatistics['type']){
   	case 0:
   		$title = 'Статистика за день';
   		$x = 'Часы';
   		break;
   	case 1:
   		$title = 'Статистика за неделю';
   		$x = 'Дни';
   		break;
   	case 2:
   		$title = 'Статистика по неделям';
   		$x = 'Недели';
   		break;
   	case 3:
   		$title = 'Статистика по месяцам';
   		$x = 'Месяцы';
   		break;
   }
   # Store chart attributes in a variable
   $strParam="caption=" . $title . ";xAxisName=" . $x . ";canvasBorderThickness=1; canvasBorderColor=cccccc; rotateNames=" . ($arrStatistics['type'] == 0 ? 1 : 0) . "; numDivLines=3; baseFontSize=11; showValues=0;formatNumber=0;";

   # Set chart attributes
   $FC->setChartParams($strParam);
   
   $size = sizeof($arrStatistics) - 1;

   switch ($arrStatistics['type']){
   	case 0:
   		for ($num = 0; $num < $size; $num++)
		   	$FC->addCategory(Service_Date::getTimeOnly($arrStatistics[$num]['datetime'], true, true, false));
   		break;
   	case 1:
   		for ($num = 0; $num < $size; $num++)
		   	$FC->addCategory(Service_Date::getCaseDateForUser($arrStatistics[$num]['datetime'], 2/*!!!! важно timezone - 1*/, 2, false, true));
   		break;
   	case 2:
   		for ($num = 0; $num < $size; $num++)
		   	$FC->addCategory(Service_Date::getNumDate($arrStatistics[$num]['datetime'], 3, 2, false, true));
   		break;
   	case 3:
   		for ($num = 0; $num < $size; $num++)
		   	$FC->addCategory(Service_Date::getCaseDateForUser($arrStatistics[$num]['datetime']));
   		break;
   }
   	
   $trueFlagComment = false;
   $trueFlagMark = false;
   $trueFlagDiff = false;
   $trueFlagArticle = false;
   $trueFlagLesson = false;
   $trueFlagAnswer = false;
   $trueFlagQuestion = false;
   $trueFlagSolution = false;
   $trueFlagUsers = false;
	 $arrComment = array();
	 $arrMark = array();
	 $arrDifficulty = array();
	 $arrArticle = array();
	 $arrLesson = array();
	 $arrAnswer = array();
	 $arrQuestion = array();
	 $arrSolution = array();
	 $arrUser = array();
   
   foreach ($arrStatistics as $arr){
   	if (!empty($arr['comment'])){
   		$arrComment[] = $arr['comment'];
   		$trueFlagComment = true;
   	}
   	else $arrComment[] = 0;
   		
   		
   	if (!empty($arr['mark'])){
   		$arrMark[] = $arr['mark'];
   		$trueFlagMark = true;
   	}
   	else $arrMark[] = 0;
   	
   		
   	if (!empty($arr['difficulty'])){
   		$arrDifficulty[] = $arr['difficulty'];
   		$trueFlagDiff = true;
   	}
   	else $arrDifficulty[] = 0;
   	
   		
   	if (!empty($arr['article'])){
   		$arrArticle[] = $arr['article'];
   		$trueFlagArticle = true;
   	}
   	else $arrArticle[] = 0;
   	
   		
   	if (!empty($arr['lesson'])){
   		$arrLesson[] = $arr['lesson'];
   		$trueFlagLesson = true;
   	}
   	else $arrLesson[] = 0;
   	
   		
   	if (!empty($arr['answer'])){
   		$arrAnswer[] = $arr['answer'];
   		$trueFlagAnswer = true;
   	}
   	else $arrAnswer[] = 0;
   	
   		
   	if (!empty($arr['question'])){
   		$arrQuestion[] = $arr['question'];
   		$trueFlagQuestion = true;
   	}
   	else $arrQuestion[] = 0;
   	
   		
   	if (!empty($arr['solution'])){
   		$arrSolution[] = $arr['solution'];
   		$trueFlagSolution = true;
   	}
   	else $arrSolution[] = 0;
   	
   		
   	if (!empty($arr['user'])){
   		$arrUser[] = $arr['user'];
   		$trueFlagUser = true;
   	}
   	else $arrUser[] = 0;
   		
   		
   		
   		
   		
   }
      
   if ($trueFlagComment){
   	$FC->addDataset("Комментарии");
   	foreach ($arrComment as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagMark){
   	$FC->addDataset("Оценки");
   	foreach ($arrMark as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagDiff){
   	$FC->addDataset("Оценки сложности");
   	foreach ($arrDifficulty as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagArticle){
   	$FC->addDataset("Статьи");
   	foreach ($arrArticle as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagLesson){
   	$FC->addDataset("Уроки");
   	foreach ($arrLesson as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagAnswer){
   	$FC->addDataset("Ответы");
   	foreach ($arrAnswer as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagQuestion){
   	$FC->addDataset("Вопросы");
   	foreach ($arrQuestion as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagSolution){
   	$FC->addDataset("Готовые решени");
   	foreach ($arrSolution as $arr)   	
   		$FC->addChartData($arr);
   }
      
   if ($trueFlagUser){
   	$FC->addDataset("Новые пользователи");
   	foreach ($arrUser as $arr)   	
   		$FC->addChartData($arr);
   }
   
   
   /*$FC->addDataset("Комментарии");
   foreach ($arrStatistics as $arr)
	   !empty($arr['comment']) ? $FC->addChartData($arr['comment']) : $FC->addChartData(0);
	   
   $FC->addDataset("Оценки");
   foreach ($arrStatistics as $arr)
	   !empty($arr['mark']) ? $FC->addChartData($arr['mark']) : $FC->addChartData(0);
	   
   $FC->addDataset("Оценки сложности");
   foreach ($arrStatistics as $arr)
	   !empty($arr['difficulty']) ? $FC->addChartData($arr['difficulty']) : $FC->addChartData(0);
	   
   $FC->addDataset("Статьи");
   foreach ($arrStatistics as $arr)
	   !empty($arr['article']) ? $FC->addChartData($arr['article']) : $FC->addChartData(0);
	   
   $FC->addDataset("Уроки");
   foreach ($arrStatistics as $arr)
	   !empty($arr['lesson']) ? $FC->addChartData($arr['lesson']) : $FC->addChartData(0);
	   
   $FC->addDataset("Ответы");
   foreach ($arrStatistics as $arr)
	   !empty($arr['answer']) ? $FC->addChartData($arr['answer']) : $FC->addChartData(0);
	   
   $FC->addDataset("Вопросы");
   foreach ($arrStatistics as $arr)
	   !empty($arr['question']) ? $FC->addChartData($arr['question']) : $FC->addChartData(0);
	   
   $FC->addDataset("Готовые решения");
   foreach ($arrStatistics as $arr)
	   !empty($arr['solution']) ? $FC->addChartData($arr['solution']) : $FC->addChartData(0);
	   
   $FC->addDataset("Новые пользователи");
   foreach ($arrStatistics as $arr)
	   !empty($arr['user']) ? $FC->addChartData($arr['user']) : $FC->addChartData(0);*/
   
   /*# Add category names
   $FC->addCategory("Week 1");
   $FC->addCategory("Week 2");
   $FC->addCategory("Week 3");
   $FC->addCategory("Week 4");

   # Add a new dataset with dataset parameters
   $FC->addDataset("This Month","showValues=0");
   # Add chart data for the above dataset
   $FC->addChartData("40800");
   $FC->addChartData("31400");
   $FC->addChartData("26700");
   $FC->addChartData("54400");

   # Add aother dataset with dataset parameters
   $FC->addDataset("Previous Month","showValues=0");
   # Add chart data for the second dataset
   $FC->addChartData("38300");
   $FC->addChartData("28400");
   $FC->addChartData("15700");
   $FC->addChartData("48100");

   # Add third dataset for the secondary axis
   $FC->addDataset("Total Quantity","parentYAxis=S");
   # Add data values for the secondary axis
   $FC->addChartData("9640");
   $FC->addChartData("70000");
   $FC->addChartData("5200");
   $FC->addChartData("18100");*/

      
 # Render Chart
 $FC->renderChart();
 echo '<br/>';

}
include(TEMPLATE . 'mainBottom.php');//закрываем основные части страницы
?>